<template>
    <div>
        <!--面包屑导航区域-->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/buyerIndex' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>上报信息</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="position">
            <el-row :gutter="20" style="margin-top:10px;">
                <el-col :span="8">
                    <div>
                        <el-card>
                            <div slot="header">
                                <span>个人中心</span>
                            </div>
                            <div class="name-role">
                            <span class="sender">
                                <el-tag type="success" effect="dark">
                                    {{this.buyer.buyerName}}
                                </el-tag>
                            </span>
                            </div>
                            <el-divider></el-divider>
                            <div class="personal-relation">
                                <div class="relation-item">登录账号:
                                    <div style="float: right; padding-right:20px;">{{this.buyer.account}}</div>
                                </div>
                            </div>
                            <div class="personal-relation">
                                <div class="relation-item">登录密码:
                                    <div style="float: right; padding-right:20px;">
                                        <el-link type="success" :underline="false" @click="openPassword">已设置</el-link>
                                    </div>
                                </div>
                            </div>
                            <div class="personal-relation">
                                <div class="relation-item">地址:
                                    <div style="float: right; padding-right:20px;">{{this.buyer.buyerAddress}}
                                    </div>
                                </div>
                            </div>
                            <div class="personal-relation">
                                <div class="relation-item">联系方式:
                                    <div style="float: right; padding-right:20px;">{{this.buyer.contact}}</div>
                                </div>
                            </div>
                            <div class="personal-relation">
                                <div class="relation-item">金额:
                                    <div style="float: right; padding-right:20px;">
                                        <el-link type="success" @click="openMoney">显示</el-link>
                                    </div>
                                </div>
                            </div>
                            <div class="personal-relation">
                                <div class="relation-item">操作:
                                    <div style="float: right; padding-right:20px;">
                                        <el-link type="primary" @click="showEditPasswordDialog">修改密码</el-link>
                                        |
                                        <el-link type="primary" @click="showEditMessageDialog">修改信息</el-link>
                                    </div>
                                </div>
                            </div>
                        </el-card>
                    </div>
                </el-col>
            </el-row>

            <el-dialog title="修改密码" :visible.sync="editPassowrdDialogVisible" width="50%"
                       @close="editPassowrdDialogClosed">
                <el-form ref="editPasswordRef" :model="editPassword" :rules="editPasswordRules" label-width="150px">
                    <el-form-item label="当前密码" prop="currentPassword">
                        <el-input v-model="editPassword.currentPassword" type="password" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newPassword">
                        <el-input v-model="editPassword.newPassword" type="password" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="重复密码" prop="repeatPassword">
                        <el-input v-model="editPassword.repeatPassword" type="password" show-password></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                <el-button @click="editPassowrdDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editBuyerPassword">确 定</el-button>
            </span>
            </el-dialog>

            <el-dialog title="修改信息" :visible.sync="editMessageDialogVisible" width="50%"
                       @close="editMessageDialogClosed">
                <el-form ref="editMessageRef" :model="editMessage" :rules="editMessageRules" label-width="150px">
                    <el-form-item label="名称" prop="buyerName">
                        <el-input v-model="editMessage.buyerName"></el-input>
                    </el-form-item>
                    <el-form-item label="联系方式" prop="contact">
                        <el-input v-model="editMessage.contact"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                <el-button @click="editMessageDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editBuyerMessage">确 定</el-button>
            </span>
            </el-dialog>
        </div>
    </div>
</template>

<script scoped>
    export default {
        data() {
            // 验证手机号的规则
            var checkMobile = (rule, value, callback) => {
                // 验证手机号的正则表达式
                const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
                if (regMobile.test(value)) {
                    return callback()
                }
                callback(new Error('请输入合法的手机号'))
            }
            //验证密码是否一致
            var checkPassword = (rule, value, callback) => {
                if (value !== this.editPassword.newPassword) {
                    callback(new Error('两次密码不一致'))
                } else {
                    return callback()
                }
            }
            return {
                account: '',
                token: '',
                buyer: [],
                password: '',
                money: '',
                //控制修改密码对话框的显示与隐藏
                editPassowrdDialogVisible: false,
                editPassword: {
                    currentPassword: '',
                    newPassword: '',
                    repeatPassword: ''
                },
                editPasswordRules: {
                    currentPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 15, message: '密码的长度在6~15个字符之间', trigger: 'blur'}
                    ],
                    newPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 15, message: '密码的长度在6~15个字符之间', trigger: 'blur'}
                    ],
                    repeatPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 15, message: '密码的长度在6~15个字符之间', trigger: 'blur'},
                        {validator: checkPassword, trigger: 'blur'}
                    ]
                },
                //控制修改信息对话框的显示与隐藏
                editMessageDialogVisible: false,
                editMessage: {
                    buyerName: '',
                    contact: ''
                },
                editMessageRules: {
                    buyerName: [
                        {required: true, message: '请输入昵称', trigger: 'blur'}
                    ],
                    contact: [
                        {required: true, message: '请输入联系方式', trigger: 'blur'},
                        {validator: checkMobile, trigger: 'blur'}
                    ]
                }
            }
        },
        async created() {
            this.token = window.sessionStorage.getItem('token');
            const result = await this.$http.get('user/buyer/getInformationFromToken', {
                params: {
                    token: this.token
                }
            })
            this.account = result.data.data.account
            this.getBuyer(result.data.data.account)
        },
        methods: {
            async getBuyer(account) {
                var result = await this.$http.get('user/buyer/selectOne', {
                    params: {
                        account: account
                    }
                })
                if (result.data.status !== 200) {
                    return this.$message.error("获取上报员信息失败！")
                }
                this.buyer = result.data.data
                this.password = result.data.data.buyerPassword
                this.money = result.data.data.money
            },
            openPassword() {
                this.$alert(this.password, '密码', {
                    confirmButtonText: '确定',
                });
            },
            openMoney() {
                this.$alert(this.money, '金额', {
                    confirmButtonText: '确定',
                });
            },
            showEditPasswordDialog() {
                this.editPassowrdDialogVisible = true
            },
            //监听修改厂家对话框的关闭事件
            editPassowrdDialogClosed() {
                this.$refs.editPasswordRef.resetFields()
            },
            //修改密码
            editBuyerPassword() {
                this.$refs.editPasswordRef.validate(async valid => {
                    if (!valid) return
                    const result = await this.$http.get('user/buyer/editPassword', {
                        params: {
                            account: this.account,
                            currentPassword: this.editPassword.currentPassword,
                            newPassword: this.editPassword.newPassword
                        }
                    })
                    if (result.data.status !== 200) {
                        return this.$message.error("当前密码输入有误，请检查！")
                    }
                    this.getBuyer(this.account)
                    // 关闭对话框
                    this.editPassowrdDialogVisible = false
                    // 提示修改成功
                    this.$message.success('修改密码成功！')
                })
            },
            showEditMessageDialog() {
                this.getBuyer(this.account)
                this.editMessage.buyerName = this.buyer.buyerName
                this.editMessage.contact = this.buyer.contact
                this.editMessageDialogVisible = true
            },
            //监听修改信息对话框的关闭事件
            editMessageDialogClosed() {
                this.$refs.editMessageRef.resetFields()
            },
            editBuyerMessage() {
                this.$refs.editMessageRef.validate(async valid => {
                    if (!valid) return
                    const result = await this.$http.get('user/buyer/editMessage', {
                        params: {
                            account: this.account,
                            buyerName: this.editMessage.buyerName,
                            contact: this.editMessage.contact
                        }
                    })
                    if (result.data.status !== 200) {
                        return this.$message.error("修改信息失败！")
                    }
                    this.getBuyer(this.account)
                    // 关闭对话框
                    this.editMessageDialogVisible = false
                    // 提示修改成功
                    this.$message.success('修改信息成功！')

                })
            }
        }
    }
</script>

<style scoped>
    .position {
        margin-top: 60px;
        margin-left: 200px;
        width: 2500px;
        height: 200px;
        font-size: 20px;
    }

    .name-role {
        font-size: 16px;
        padding: 5px;
        text-align: center;
    }

    .sender {
        text-align: center;
    }

    .personal-relation {
        font-size: 16px;
        padding: 0px 5px 15px;
        margin-right: 1px;
        width: 100%
    }

    .relation-item {
        padding: 12px;

    }
</style>
